package com.szly.phm.admin.security;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description : ${创建类}
 * @Author : chengjiang
 * @Date: 2021/7/9 11:17
 */
@Component
public class JEJLNotLoginHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        var jsonObject = new JSONObject();
        jsonObject.put("code", "401");
        jsonObject.put("message", "用户未登录，请先登录");
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json");
        httpServletResponse.getWriter().write(jsonObject.toJSONString());
    }
}
